//
//  Gradekeeper0_2AppDelegate.m
//  Gradekeeper0.2
//
//  Created by Libo Zhou on 10/22/09.
//  Copyright 2009 i5. All rights reserved.
//

#import "Gradekeeper0_2AppDelegate.h"
#import "ProfileNavsController.h"
//<<<<<<< .mine
#import "SemNavsController.h"
#import "CourseNav.h"
//=======
#import "Todo.h"
#import "TaskListViewController.h"
//>>>>>>> .r33
#import "SyncViewController.h"

@interface Gradekeeper0_2AppDelegate (Private)
- (void)createEditableCopyOfDatabaseIfNeeded;
- (void)initializeDatabase;
@end

@implementation Gradekeeper0_2AppDelegate

@synthesize window;
@synthesize rootController;
@synthesize pNavsController;
@synthesize cNavsController;
@synthesize course;
@synthesize sNavsContoller;
@synthesize todos;
@synthesize ViewController;


- (id)init {
	if (self = [super init]) {
		// 
	}
	return self;
}

- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
	[splashView removeFromSuperview];
	[splashView release];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
	[self createEditableCopyOfDatabaseIfNeeded];
	[self initializeDatabase];
	
    // Override point for customization after application launch
    [window addSubview:rootController.view];
	[window makeKeyAndVisible];
	
	splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
	splashView.image = [UIImage imageNamed:@"Default.png"];
	[window addSubview:splashView];
	[window bringSubviewToFront:splashView];
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:window cache:YES];
	[UIView setAnimationDelegate:self]; 
	[UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
	splashView.alpha = 0.0;
	splashView.frame = CGRectMake(-60, -60, 440, 600);
	[UIView commitAnimations];
}

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"todo.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"todo.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
	
}

// Open the database connection and retrieve minimal information for all objects.
- (void)initializeDatabase {
    NSMutableArray *todoArray = [[NSMutableArray alloc] init];
    self.todos = todoArray;
    [todoArray release];
    // The database is stored in the application bundle. 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"todo.sqlite"];
    // Open the database. The database was prepared outside the application.
    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
        // Get the primary key for all books.
        const char *sql = "SELECT pk FROM todo";
        sqlite3_stmt *statement;
        // Preparing a statement compiles the SQL query into a byte-code program in the SQLite library.
        // The third parameter is either the length of the SQL string or -1 to read up to the first null terminator.        
        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
            // We "step" through the results - once for each row.
            while (sqlite3_step(statement) == SQLITE_ROW) {
                // The second parameter indicates the column index into the result set.
                int primaryKey = sqlite3_column_int(statement, 0);
                // We avoid the alloc-init-autorelease pattern here because we are in a tight loop and
                // autorelease is slightly more expensive than release. This design choice has nothing to do with
                // actual memory management - at the end of this block of code, all the book objects allocated
                // here will be in memory regardless of whether we use autorelease or release, because they are
                // retained by the books array.
                Todo *td = [[Todo alloc] initWithPrimaryKey:primaryKey database:database];
				
                [todos addObject:td];
                [td release];
            }
        }
        // "Finalize" the statement - releases the resources associated with the statement.
        sqlite3_finalize(statement);
    } else {
        // Even though the open failed, call close to properly clean up resources.
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        // Additional error handling, as appropriate...
    }
}

-(void)removeTodo:(Todo *)todo {
	NSUInteger index = [todos indexOfObject:todo];
	
    if (index == NSNotFound) return;
	
    [todo deleteFromDatabase];
    [todos removeObject:todo];
}

-(Todo *) addTodo {
	NSInteger primaryKey = [Todo insertNewTodoIntoDatabase:database];
    Todo *newTodo = [[Todo alloc] initWithPrimaryKey:primaryKey database:database];
	
	[todos addObject:newTodo];
	return newTodo;
}

- (void)applicationWillTerminate:(UIApplication *)application {
	// Save data if appropriate
	[todos makeObjectsPerformSelector:@selector(dehydrate)];
}


- (void)dealloc {

	[cNavsController release];
	[pNavsController release];
	[sNavsContoller release];
	[course release];
	[rootController release];
    [window release];
    [super dealloc];
}


@end
